import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';

import { Observable, Subject } from 'rxjs';
import { MaterialDialogService } from '../services/material-dialog.service';

export interface CanComponentDeactivate {
    canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable()
export class CanDeactivateGuardService implements CanDeactivate<CanComponentDeactivate> {
    private exit: Subject<boolean> = new Subject();

    constructor(private materialDialogService: MaterialDialogService) {

    }

    canDeactivate(component: CanComponentDeactivate): boolean {
        return true;
    }
}

// Alternatively, you could make a component-specific CanDeactivate guard for the CrisisDetailComponent. 
// The canDeactivate() method provides you with the current instance of the component, the current ActivatedRoute, 
// and RouterStateSnapshot in case you needed to access some external information. This would be useful if you 
// only wanted to use this guard for this component and needed to get the component's properties or confirm whether 
// the router should allow navigation away from it.

// import { Injectable } from '@angular/core';
// import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

// import { CrisisDetailComponent } from '../../crisis-center/crisis-detail.component';

// @Injectable()
// export class CanDeactivateGuard implements CanDeactivate<CrisisDetailComponent> {
//     canDeactivate(
//         component: CrisisDetailComponent,
//         route: ActivatedRouteSnapshot,
//         state: RouterStateSnapshot
//     ): Promise<boolean> | boolean {
//         // Get the Crisis Center ID
//         console.log(route.paramMap.get('id'));

//         // Get the current URL
//         console.log(state.url);

//         // Allow synchronous navigation (`true`) if no crisis or the crisis is unchanged
//         if (!component.crisis || component.crisis.name === component.editName) {
//             return true;
//         }

//         // Otherwise ask the user with the dialog service and return its
//         // promise which resolves to true or false when the user decides
//         return component.dialogService.confirm('Discard changes?');
//     }
// }